package org.xtext.ic.dsl.generator;

import com.google.common.base.Objects;
import org.eclipse.emf.common.util.EList;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.xbase.lib.StringExtensions;
import org.xtext.ic.dsl.IcDslExtensions;
import org.xtext.ic.dsl.icDsl.Attribute;
import org.xtext.ic.dsl.icDsl.Entity;
import org.xtext.ic.dsl.icDsl.Model;
import org.xtext.ic.dsl.icDsl.Type;
import org.xtext.ic.dsl.icDsl.TypeDef;

@SuppressWarnings("all")
public class ModelExtensions extends IcDslExtensions {
  public CharSequence compile(final Model m, final Entity e) {
    StringConcatenation _builder = new StringConcatenation();
    CharSequence _packageHeader = this.packageHeader(m, e);
    _builder.append(_packageHeader, "");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    String _imports = this.imports(m, e);
    _builder.append(_imports, "");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    CharSequence _classes = this.classes(e);
    _builder.append(_classes, "");
    _builder.newLineIfNotEmpty();
    return _builder;
  }
  
  public String imports(final Model m, final Entity e) {
    String _xblockexpression = null;
    {
      String importList = this.importAnnotations(e);
      boolean pass = false;
      EList<Type> _types = m.getTypes();
      for (final Type t : _types) {
        if ((t instanceof TypeDef)) {
          TypeDef td = ((TypeDef) t);
          String _mappedType = td.getMappedType();
          boolean _isNullOrEmpty = StringExtensions.isNullOrEmpty(_mappedType);
          boolean _not = (!_isNullOrEmpty);
          if (_not) {
            pass = false;
            EList<Attribute> _attributes = e.getAttributes();
            for (final Attribute a : _attributes) {
              boolean _and = false;
              boolean _not_1 = (!pass);
              if (!_not_1) {
                _and = false;
              } else {
                String _name = td.getName();
                Type _type = a.getType();
                String _name_1 = _type.getName();
                boolean _equals = _name.equals(_name_1);
                _and = (_not_1 && _equals);
              }
              if (_and) {
                String _plus = (importList + "import ");
                String _mappedType_1 = td.getMappedType();
                String _plus_1 = (_plus + _mappedType_1);
                String _plus_2 = (_plus_1 + ";");
                String _property = System.getProperty("line.separator");
                String _plus_3 = (_plus_2 + _property);
                importList = _plus_3;
                pass = true;
              }
            }
          }
        }
      }
      pass = false;
      EList<Attribute> _attributes_1 = e.getAttributes();
      for (final Attribute a_1 : _attributes_1) {
        boolean _and_1 = false;
        boolean _not_2 = (!pass);
        if (!_not_2) {
          _and_1 = false;
        } else {
          boolean _isMany = a_1.isMany();
          _and_1 = (_not_2 && _isMany);
        }
        if (_and_1) {
          String _plus_4 = (importList + "import java.util.List;");
          String _property_1 = System.getProperty("line.separator");
          String _plus_5 = (_plus_4 + _property_1);
          importList = _plus_5;
          pass = true;
          return importList;
        }
      }
      _xblockexpression = (importList);
    }
    return _xblockexpression;
  }
  
  public CharSequence classes(final Entity e) {
    StringConcatenation _builder = new StringConcatenation();
    String _verifyAnnotations = this.verifyAnnotations(e);
    _builder.append(_verifyAnnotations, "");
    _builder.newLineIfNotEmpty();
    _builder.append("public class ");
    String _name = e.getName();
    _builder.append(_name, "");
    {
      Entity _superEntity = e.getSuperEntity();
      boolean _notEquals = (!Objects.equal(_superEntity, null));
      if (_notEquals) {
        _builder.append(" extends ");
        Entity _superEntity_1 = e.getSuperEntity();
        String _name_1 = _superEntity_1.getName();
        _builder.append(_name_1, "");
      }
    }
    _builder.append("{");
    _builder.newLineIfNotEmpty();
    _builder.append("\t");
    _builder.newLine();
    {
      EList<Attribute> _attributes = e.getAttributes();
      for(final Attribute a : _attributes) {
        _builder.append("\t");
        _builder.append("private ");
        CharSequence _variable = this.variable(a);
        _builder.append(_variable, "	");
        _builder.append(";");
        _builder.newLineIfNotEmpty();
      }
    }
    {
      EList<Attribute> _attributes_1 = e.getAttributes();
      for(final Attribute a_1 : _attributes_1) {
        _builder.append("\t");
        _builder.newLine();
        _builder.append("\t");
        String _verifyAnnotations_1 = this.verifyAnnotations(a_1);
        _builder.append(_verifyAnnotations_1, "	");
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        _builder.append("public ");
        CharSequence _typeDeclaration = this.typeDeclaration(a_1);
        _builder.append(_typeDeclaration, "	");
        _builder.append(" get");
        String _name_2 = a_1.getName();
        String _firstUpper = StringExtensions.toFirstUpper(_name_2);
        _builder.append(_firstUpper, "	");
        _builder.append("(){");
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        _builder.append("\t");
        _builder.append("return ");
        String _name_3 = a_1.getName();
        _builder.append(_name_3, "		");
        _builder.append(";");
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        _builder.append("}");
        _builder.newLine();
        _builder.append("\t");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("public void set");
        String _name_4 = a_1.getName();
        String _firstUpper_1 = StringExtensions.toFirstUpper(_name_4);
        _builder.append(_firstUpper_1, "	");
        _builder.append("(");
        CharSequence _variable_1 = this.variable(a_1);
        _builder.append(_variable_1, "	");
        _builder.append("){");
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        _builder.append("\t");
        _builder.append("this.");
        String _name_5 = a_1.getName();
        _builder.append(_name_5, "		");
        _builder.append(" = ");
        String _name_6 = a_1.getName();
        _builder.append(_name_6, "		");
        _builder.append(";");
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        _builder.append("}");
        _builder.newLine();
      }
    }
    _builder.append("\t");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    return _builder;
  }
}
